﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.CallRoute;


public partial class masterdata_VisitFrequencyList : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
            GetVisitFrequencyBind();
        //}
    }

    public void GetVisitFrequencyBind()
    {
        gvList.DataSource = new VisitFrequencyManager().LoadAll(MyStateManager.DomainID);
        gvList.DataBind();
    }

    /// <summary>
    /// 保存方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        VisitFrequencyManager manager = new VisitFrequencyManager();
        string LogContent = "";
        try
        {
            Jumpton.SFA.Server.Entity.VisitFrequency frequency = null;
            if (gvList.SelectedIndex != -1)
            {
                frequency = manager.Load(int.Parse(gvList.DataKeys[gvList.SelectedIndex].Value.ToString()));
            }
            else
            {
                frequency = new Jumpton.SFA.Server.Entity.VisitFrequency();
                frequency.TypeID = decimal.Parse(ddlType.Items[ddlType.SelectedIndex].Value);

            }
            frequency.UpdateBy = MyStateManager.Name;
            frequency.LastUpdate = DateTime.Now;
            frequency.IsActive = chkIsActive.Checked;
            frequency.IsCheck = chkIsChecked.Checked;
            frequency.Cycle = byte.Parse(ddlCycle.SelectedValue);
            frequency.Frequency = txtFrequency.Value;
            frequency.DomainID = MyStateManager.DomainID;

            LogContent += "客户类型：" + frequency.TypeID + "，";
            LogContent += "拜访周期：" + frequency.Cycle + "，";
            LogContent += "拜访频率：" + frequency.Frequency + "，";
            LogContent += "有效：" + frequency.IsActive + "，";
            LogContent += "强制规范：" + frequency.IsCheck + "。";

            manager.Save(frequency);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
              this.MyStateManager.UserID, LogContent);
            GetVisitFrequencyBind();
            btnCancel_Click(sender, e);
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
              this.MyStateManager.UserID, "新增拜访频率 - 异常:" + ex.Message);
            ShowMessage(UpdatePanel1, ex.Message);
        }

    }

    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        editArea.Visible = false;
        back.Attributes.Remove("class");
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
       
        var frequency = new VisitFrequencyManager().LoadView(int.Parse( gvList.DataKeys[gvList.SelectedIndex].Value.ToString() ));
        if(frequency != null)
        {
            txtFrequency.Value = frequency.Frequency.ToString();
            ddlCycle.SelectedValue = frequency.Cycle.ToString();
            switch(ddlCycle.SelectedIndex)
            {
                case 0:
                    rv2.MaximumValue = "7";
                    break;
                case 1:
                    rv2.MaximumValue = "28";
                    break;
                case 2:
                    rv2.MaximumValue = "365";
                    break;
            }
            lblCustomerType.Text = frequency.CustomerTypeName;
            ddlType.Visible = false;
            lblCustomerType.Visible = true;
            chkIsActive.Checked = frequency.IsActive;
            chkIsChecked.Checked = frequency.IsCheck;
           
        }
        //绑定数据
        SetStatus();

    }
  

    private void SetStatus()
    {
        editArea.Visible = true;
        editArea.Style.Add("width", "450px");
        back.Attributes.Add("class", "back");
    }

    //新增
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        gvList.SelectedIndex = -1;
        ddlType.Visible = true;
        lblCustomerType.Visible = false;
        txtFrequency.Value = "";
        chkIsActive.Checked = true;
        rv2.MaximumValue = "7";
        //绑定客户类型列表
        ddlType.DataSource = new VisitFrequencyManager().LoadCustomerType(MyStateManager.DomainID);
        ddlType.DataTextField = "Name";
        ddlType.DataValueField = "DictionaryItemid";
        ddlType.DataBind();
        SetStatus();
    }


    protected void ddlCycle_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlCycle.SelectedIndex)
        {
            case 0:
                rv2.MaximumValue = "7";
                break;
            case 1:
                rv2.MaximumValue = "28";
                break;
            case 2:
                rv2.MaximumValue = "365";
                break;
        }
    }
}
